<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像 -->
      <div class="avatar-box">
        <img src="../assets/imgs/123.jpg" alt="">
      </div>
      <!-- 登陆表单 -->
      <el-form :model="loginForm" :rules="rules" ref='loginForm' class="login-form">
        <el-form-item prop='username'>
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <i slot="prefix" class="el-input__icon el-icon-s-custom"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop='password'>
          <el-input v-model="loginForm.password" placeholder="请输入密码">
            <i slot="prefix" class="el-input__icon el-icon-question"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click='login'>登录</el-button>
          <el-button type="info" @click='reset'>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  // 1、对表单数据进行双向数据绑定
  // 2、对双向数据绑定的数据进行校验
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    reset() {
      this.$refs.loginForm.resetFields()
    },
    // 登录
    login() {
      this.$refs.loginForm.validate(async flag => {
        if (!flag) return this.$message.error('用户名或密码错误')
        // 发起axios登录请求 获取数据
        const { data } = await this.$http.post('login', this.loginForm)
        // 登录失败校验
        if (data.meta.status !== 200) return this.$message.error('登录失败')
        // 登录成功
        // 1、将token存储到sessionStroage中 除了login请求其它的都要携带token
        sessionStorage.setItem('token', data.data.token)
        // 2、弹窗提示登录成功
        this.$message({
          message: '登录成功 ~',
          type: 'success'
        })
        // 3、跳转到/home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less">
.login-container {
  // background-color: slategray;
  background: url('../assets/imgs/bg.jpg') no-repeat;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-size: cover;
  .login-box {
    width: 500px;
    height: 350px;
    background-color: #fff;
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    box-shadow: 5px 5px 5px #ddd;
  }
  .avatar-box {
    width: 120px;
    height: 120px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 5px 5px 5px #ddd;
    img {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login-form {
    position: absolute;
    bottom: 40px;
    padding: 0 20px;
    width: 100%;
    box-sizing: border-box;
    .el-input__inner {
      height: 48px;
      font-size: 16px;
    }
    .el-input__icon {
      font-size: 16px;
      line-height: 50px;
    }
  }
  .btns {
    display: flex;
    justify-content: space-between;
  }
}
</style>